package com.etoak.controller;

import com.etoak.CartDTO;
import com.etoak.common.core.vo.ResultVO;
import com.etoak.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    CartService cartService;

    @PostMapping
    public ResultVO<Object> add(@Validated @RequestBody CartDTO cartDTO) {
        cartService.add(cartDTO);
        return ResultVO.success();
    }

    /**
     *  查询购物车中的菜品列表
     */

    @GetMapping
    public ResultVO<List<CartDTO>> List() {
        List<CartDTO> list = cartService.getList();
        return ResultVO.success(list);
    }

    @DeleteMapping("/dishes/{id}")
    public ResultVO<Object> deleteDishes(@PathVariable int id) {
        cartService.delete(id);
        return ResultVO.success();
    }

    @DeleteMapping
    public ResultVO<Object> clearCart() {
        cartService.clearCart();
        return ResultVO.success();
    }
}
